package cn.aran.org.service.impl;

import cn.aran.basic.exception.BusinessException;
import cn.aran.basic.service.impl.BaseServiceImpl;
import cn.aran.basic.utils.BaiduAuditUtils;
import cn.aran.basic.utils.MailUtils;
import cn.aran.org.domain.Employee;
import cn.aran.org.domain.Shop;
import cn.aran.org.domain.ShopAuditLog;
import cn.aran.org.mapper.EmployeeMapper;
import cn.aran.org.mapper.ShopAuditLogMapper;
import cn.aran.org.mapper.ShopMapper;
import cn.aran.org.service.IShopService;
import cn.aran.org.vo.ShopEchartVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @Description: 店铺实现类
* @Author: Aran
* @Version: 1.0
* @Date:  2022/7/2 13:14
*/
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private EmployeeServiceImpl employeeService;

    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;

    @Autowired
    private JavaMailSender javaMailSender;

    @Override
    public void settlement(Shop shop) {
        // 判断传入参数是否合法
        if (StringUtils.isEmpty(shop.getName())){
            throw new BusinessException("店铺名称不能为空!");
        }
        if (StringUtils.isEmpty(shop.getTel())){
            throw new BusinessException("店铺号码不能为空!");
        }
        if (StringUtils.isEmpty(shop.getAddress())){
            throw new BusinessException("店铺地址不能为空!");
        }
        if (StringUtils.isEmpty(shop.getLogo())){
            throw new BusinessException("店铺logo不能为空!");
        }

        // 校验名称是否合规
        Boolean nameCensor = BaiduAuditUtils.TextCensor(shop.getName());
        if (!nameCensor){
            throw new BusinessException("店铺名称敏感,请重新输入!");
        }

        // 判断店铺名是否已存在
        Shop loadShop = shopMapper.loadByName(shop.getName());
        if(loadShop !=null){
            throw new BusinessException("店铺已入驻!");
        }

        Employee shopAdmin = shop.getAdmin();
        // 判断密码是否相等
        if (!shopAdmin.getPassword().equals(shopAdmin.getComfirmPassword())){
            throw new BusinessException("密码不一致!");
        }
        // 判断管理员是否注册并绑定了店铺
        Employee shopEmployee = employeeMapper.loadByUsername(shopAdmin.getUsername());
        if (shopEmployee != null && shopMapper.loadByAdminId(shopEmployee.getId()) != null){
            throw new BusinessException("管理员已注册!");
        }

        // 注册管理员
        if (shopEmployee == null){
            employeeService.save(shopAdmin);
        }

        // 注册店铺
        // 设置店铺管理员id
        shop.setAdminId(shopAdmin.getId());
        shopMapper.save(shop);
        // 设置管理员店铺id
        shopAdmin.setShopId(shop.getId());
        employeeService.update(shopAdmin);


    }

    /**
    * @Title: pass
    * @Description: 店铺审核通过
    * @Author: Aran
    * @Version: 1.0
    * @Date:  2022/7/3 9:31
    * @Parameters: [shopAuditLog]
    * @Return void
    */
    @Override
    public void pass(ShopAuditLog shopAuditLog) throws MessagingException {
        // 设置操作人
        shopAuditLog.setAuditId(351);
        // 审核通过,设置状态为待激活
        Shop shop = shopMapper.loadById(shopAuditLog.getShopId().longValue());
        shop.setState(1);
        shopMapper.update(shop);
        // 保存日志信息
        shopAuditLogMapper.save(shopAuditLog);
        // 设置店铺信息为已通过
        // 发送激活邮件
        // String email = shop.getAdmin().getEmail();
        String text = "<h1>您的店铺已通过审核,请点击链接进行店铺激活</h1>"+
                "激活链接: <a href='http://localhost:8081/#/activation?shopId="+shopAuditLog.getShopId()+"'>点我激活店铺</a>";
        String title =  "宠物乐园店铺入驻结果通知";
        MailUtils.sendMail("lzy2462707348@163.com", text, title);
        // // 1.创建复杂邮件对象
        // MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        // MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        // helper.setFrom("lzy2462707348@163.com");
        // helper.setSubject("宠物乐园店铺入驻结果通知");
        // // 3.设置文本内容，第二个参数 true 代表是否是一个html内容
        // helper.setText("<h1>您的店铺已通过审核,请点击链接进行店铺激活</h1>"+
        //         "激活链接: <a href='http://localhost:8081/#/activation?shopId="+shopAuditLog.getShopId()+"'>点我激活店铺</a>",true);
        // // 5.设置收件人
        // helper.setTo("lzy2462707348@163.com");
        // // 6.发送邮件
        // javaMailSender.send(mimeMessage);
    }

    /**
    * @Title: active
    * @Description: 店铺激活
    * @Author: Aran
    * @Version: 1.0
    * @Date:  2022/7/3 15:31
    * @Parameters: [id]
    * @Return void
    */
    @Override
    public void active(Long id) {
        Shop shop = shopMapper.loadById(id);
        shop.setState(2);
        shopMapper.update(shop);
    }

    /**
    * @Title: reject
    * @Description: 店铺审核驳回
    * @Author: Aran
    * @Version: 1.0
    * @Date:  2022/7/3 9:31
    * @Parameters: [shopAuditLog]
    * @Return void
    */
    @Override
    public void reject(ShopAuditLog shopAuditLog) throws MessagingException {
        // 设置操作人
        shopAuditLog.setAuditId(351);
        // 审核通过,设置状态为已驳回
        Shop shop = shopMapper.loadById(shopAuditLog.getShopId().longValue());
        shop.setState(-1);
        shopMapper.update(shop);
        // 保存日志信息
        shopAuditLogMapper.save(shopAuditLog);
        // 设置店铺信息为已驳回
        // 发送邮件
        // MailUtil.sendRejectEmail("lzy2462707348@163.com",shopAuditLog.getShopId());
        String text = "<h1>亲，您的店铺入驻申请信息我平台已审核，但是因为内容不合规被驳回，请修改店铺信息后，再次申请</h1>"+
                "店铺信息修改链接：<a href='http://localhost:8081/#/registerSecondary?shopId="+shopAuditLog.getShopId()+"'>点我修改店铺信息</a>";
        String title =  "宠物乐园店铺入驻结果通知";
        MailUtils.sendMail("lzy2462707348@163.com", text, title);
        // // 1.创建复杂邮件对象
        // MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        // MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        // helper.setFrom("lzy2462707348@163.com");
        // helper.setSubject("宠物乐园店铺入驻结果通知");
        // // 3.设置文本内容，第二个参数 true 代表是否是一个html内容
        // helper.setText("<h1>亲，您的店铺入驻申请信息我平台已审核，但是因为内容不合规被驳回，请修改店铺信息后，再次申请</h1>"+
        //         "店铺信息修改链接：<a href='http://localhost:8081/#/registerSecondary?shopId="+shopAuditLog.getShopId()+"'>点我修改店铺信息</a>",true);
        // // 5.设置收件人
        // helper.setTo("lzy2462707348@163.com");
        // // 6.发送邮件
        // javaMailSender.send(mimeMessage);
    }

    /**
    * @Title: refuse
    * @Description: 店铺审核拒绝
    * @Author: Aran
    * @Version: 1.0
    * @Date:  2022/7/3 15:58
    * @Parameters: [shopAuditLog]
    * @Return void
    */
    @Override
    public void refuse(ShopAuditLog shopAuditLog) throws MessagingException {
        // 设置操作人
        shopAuditLog.setAuditId(351);
        // 审核通过,设置状态为已拒绝
        Shop shop = shopMapper.loadById(shopAuditLog.getShopId().longValue());
        shop.setState(-2);
        shopMapper.update(shop);
        // 保存日志信息
        shopAuditLogMapper.save(shopAuditLog);
        // 发送邮件
        String text = "<h1>亲，您的店铺入驻申请信息我平台已审核，但是因为内容不合规被拒绝!</h1>";
        String title =  "宠物乐园店铺入驻结果通知";
        MailUtils.sendMail("lzy2462707348@163.com", text, title);
        // //1.创建复杂邮件对象
        // MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        // MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        // helper.setFrom("lzy2462707348@163.com");
        // helper.setSubject("宠物乐园店铺入驻结果通知");
        // // 3.设置文本内容，第二个参数 true 代表是否是一个html内容
        // helper.setText("<h1>亲，您的店铺入驻申请信息我平台已审核，但是因为内容不合规被拒绝!</h1>",true);
        // // 5.设置收件人
        // helper.setTo("lzy2462707348@163.com");
        // // 6.发送邮件
        // javaMailSender.send(mimeMessage);
    }

    @Override
    public void batchSave(List<Shop> shops) {
        shopMapper.batchSave(shops);
    }

    /**
    * @Title: loadEcharts
    * @Description: 店铺报表数据
    * @Author: Aran
    * @Version: 1.0
    * @Date:  2022/7/3 15:28
    * @Parameters: []
    * @Return java.util.Map<java.lang.String,java.lang.Object>
    */
    @Override
    public Map<String, Object> loadEcharts() {
        List<ShopEchartVo> shopEchartVos = shopMapper.loadEcharts();
        List<String> states = new ArrayList<>();
        for(ShopEchartVo shop: shopEchartVos){
            if (shop.getState() == -2){
                states.add("拒绝");
            } else if (shop.getState() == -1){
                states.add("驳回");
            }else if (shop.getState() == 1){
                states.add("已审核(待激活)");
            } else if (shop.getState() == 2){
                states.add("已激活");
            } else {
                states.add("待审核");
            }
        }
        // List<Integer> states = shopEchartVos.stream().map(ShopEchartVo::getState).collect(Collectors.toList());
        List<Integer> counts = shopEchartVos.stream().map(ShopEchartVo::getCount).collect(Collectors.toList());
        /*List<Integer> states = new ArrayList<>();
        List<Integer> counts = new ArrayList<>();
        for (ShopEchartVo shopEchartVo : shopEchartVos) {
            states.add(shopEchartVo.getState());
            counts.add(shopEchartVo.getCount());
        }*/
        HashMap<String, Object> map = new HashMap<>();
        map.put("x", states);
        map.put("y", counts);
        return map;
    }


}
